﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using FrameWork.Source.Object_Classes;
using Microsoft.Xna.Framework;
using FrameWork.Source.Animation_Classes;
using FrameWork.Source.Texture;
using FrameWork.Source.Resource_Manager;
using Boomer.Source.TiledMap;
using FrameWork.Source.Utilities;
using Boomer.Source.MapItem;

namespace Boomer.Source.Breakable_Object
{
    class CBreakableObject : CAnimatedObject
    {
        public Vector2 mapPos;
        private CTiledMap map;
        public bool isBreaking { get; set; }
        private int type;

        public CBreakableObject()
            : base()
        {

        }

        public override void init()
        {
            CAnimation[] pAnim;
            pAnim = new CAnimation[4];

            pAnim[0] = new CAnimation();
            pAnim[0].createAnimation("Normal Orange", 0, 0, 0, 1.0f);

            pAnim[1] = new CAnimation();
            pAnim[1].createAnimation("Break Orange", 0, 7, 7, 1.0f / 25.0f);

            pAnim[2] = new CAnimation();
            pAnim[2].createAnimation("Normal Red", 8, 8, 8, 1.0f);

            pAnim[3] = new CAnimation();
            pAnim[3].createAnimation("Break Red", 8, 15, 15, 1.0f / 25.0f);

            CTexture image = CResourceManager.getInstance().getImage((int)CResourceManager.IMG_MAIN.BREAKABLE_OBJECT);

            this.loadSprite(image, 16, 8, 50 , 55, pAnim);

            base.init();
        }

        public override void update(float deltaTime)
        {
            if (isBreaking)
            {
                if (this.getCurrAnimation().isEndAnimation())
                {
                    isBreaking = false;
                    m_isAlive = false;
                    map.setCell((int)mapPos.X, (int)mapPos.Y, (int)CTiledMap.MAP_TYPE.GRASS);
                    addItem();
                }
            }

            

            base.update(deltaTime);
        }

        private void addItem()
        {
            int chance = CUtilities.getInstance().randomNumber(0, 101);

            if (chance <= 100)
            {
                CMapItem item = new CMapItem();
                item.init();
                item.m_vec2Position = new Vector2( this.m_vec2Position.X + 5  , this.m_vec2Position.Y );
                item.m_fDeep = this.m_fDeep;

                if (chance <= 100)
                {
                    // Init boom
                    int chanceItem = CUtilities.getInstance().randomNumber(0, 101);

                    if (chanceItem <= 50)
                    {
                        //Init boom count
                        item.setType(CMapItem.TYPE.BOOMNUM);
                    }
                    else
                    {
                        //Init boom Str
                        item.setType(CMapItem.TYPE.BOOMSTR);
                    }
                }
                if (chance <= 50)
                {
                    //Init speed
                    item.setType(CMapItem.TYPE.MOVSPEED);
                }

                MapItemManager.getInstance().addItem(item);
            }
        }

        private void setType(int type)
        {
            if (type == (int)CTiledMap.MAP_TYPE.BREAK_BOX_ORANGE)
            {
                this.changeAnimation("Normal Orange");
            }
            if (type == (int)CTiledMap.MAP_TYPE.BREAK_BOX_RED)
            {
                this.changeAnimation("Normal Red");
            }
        }

        public void breaking(bool isBreak)
        {
            isBreaking = isBreak;
            if (isBreak)
            {
                if (type == (int)CTiledMap.MAP_TYPE.BREAK_BOX_ORANGE)
                {
                    this.changeAnimation("Break Orange");
                }
                if (type == (int)CTiledMap.MAP_TYPE.BREAK_BOX_RED)
                {
                    this.changeAnimation("Break Red");
                }
                this.getCurrAnimation().resetAnimation();
            }
            else
            {
                this.changeAnimation("Normal");
            }
        }


        public void attachMap(CTiledMap map , int row , int col , int type)
        {
            this.map = map;

            float xPos = map.m_vecMapPos.X + col * map.m_iCellSize;
            float yPos = map.m_vecMapPos.Y + row * map.m_iCellSize;

            mapPos = new Vector2(row, col);
            this.m_vec2Position = new Vector2(xPos, yPos - 5);

            setType(type);

            this.type = type;

            CTiledMap.updateDeep updateDeep = new CTiledMap.updateDeep(CTiledMap.updateDeepValue);

            this.m_fDeep = updateDeep(new Vector2(m_vec2Position.X, m_vec2Position.Y + 5), map);
        }
    }
}
